TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Whats the error? (http://www.talkphp.com/general/4160-whats-error.html)

shankar 04-19-2009 05:13 AM

Whats the error?
 
<?php

session_start();

$username=$_SESSION['username'];
$fname;
$lname;
$guardian;
$connect = mysql_connect("localhost","root","") or die ("Couldn't connect");
mysql_select_db("school_inspirations") or die("Couldn't find db");

$fname = mysql_query("SELECT fname FROM student_reg WHERE username='$username'");
mysql_query($fname);


$guardian = mysql_query("SELECT guardian FROM student_reg WHERE username='$username'");
mysql_query($guardian);


?>

<table border=1px;>

<tr>
<td>
Fullname
</td>
<td>
<?php echo "$fname"; ?>
</td>
</tr>

</table>




The ouput in the table is
Resource id #3

Sakakuchi 04-19-2009 09:37 AM

wouw dude, hold on.

Please read once over your code:


Quote:

$fname = mysql_query("SELECT fname FROM student_reg WHERE username='$username'");
mysql_query($fname);
you are making a mysql_query -> and save the result int $fname.
then you trigger a mysql_query from the result you got from the sql query:

the code should look like that:

PHP Code:

//make a sql_query and save the result from that into $result
$resultmysql_query("SELECT `fname` FROM `student_reg` WHERE `username` = '$username'");

//next we need to fetch the data out of our result. Now it depends on what you want to do next with the data you received. Any of the below code could be right  -  depending on your what you wanna do with it

$array mysql_fetch_array($result); //would save result in a array
$objectmysql_fetch_object($result); //would save it into a object

$associativeArray mysql_fetch_assoc($result);

$field mysql_fetch_field($result); //get a single field

//next give out e.g.

echo $array['fname'];
echo 
$object->fname;

//...... 

Read that up on php.net or elsewhere ;) Then you understand best...

Your error means simply that you dont have an string available - so you cannot print/echo it

Tanax 04-19-2009 11:24 AM

@up- Better for the query part is to use it like this:
PHP Code:

$resultmysql_query("SELECT `fname` FROM `student_reg` WHERE `username` = '" $username "'"); 

@topicstarter- Please use php code tags [php] and also you don't have to post it on 3 places.


All times are GMT. The time now is 05:20 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0