I am new to php and mysql (like a lot of people on here), and I have been noting this strange behavior:
mysql_fetch() in my php code seems to only execute AFTER I manually open up the Mysql command client and do the query. Below is the pertinent php code where I post a username and pass (uname and pass) to this php file and then use them to query for information pertaining to that user.
Code: Select all
<head>
<?php
session_start();
?>
<head>
<?php
if (!empty($_SESSION)){
echo "Welcome ".$_SESSION['firstname'];
}
else{
$con=mysql_connect(null,'ychou','cockroach');
if(!$con)
{
die('Could not connect: '. mysql_error());
}
mysql_select_db("music_db", $con);
$uname=mysql_real_escape_string($_POST["uname"],mysql_connect(null,'ychou','cockroach'));
$pass=mysql_real_escape_string($_POST["pass"],mysql_connect(null,'ychou','cockroach'));
$query = "SELECT firstname FROM members WHERE username= '$uname' AND userpass= '$pass'";
$result = mysql_query($query,$con);
if (!$result) {
die ('Query failed'. mysql_error());
}
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result))
{
$_SESSION['fname'] = $row['firstname'];
$_SESSION['lname']= $row['lastname'];
$_SESSION['band'] = $row['band'];
$_SESSION['bandmems']= $row['bandMembers'];
$_SESSION['musicexp']= $row['musicexp'];
$_SESSION['uname'] = $uname;
$_SESSION['password'] = $pass;
}
}
echo "Welcome back, ". $row['firstname'];
}
?>Welcome back Tom.
(Assuming the firstname of user is Tom).
I had the Mysql client open and logged into it using the root user. I also have sql developer installed concurrently and notice on task manager that mysqld is always runnign concurrently with mysql. Could there be some problem regarding how I connect to the mysql server?
Thanks, and sorry for not being able to post the code in highlighting because I dont have an editor.