0

I am posting very simple PHP/mysql as below, to get the hang of creating a DB query, and checking if my syntax, and contruction logic is correct, However when I post the code below:

$dbhost = 'localhost';
$dbuser = 'myuser';
$dbpss = 'myassword';
//connect to database//
$conn = mysql_connect($dbhost, $dbuser, $dbpss) or die('Error connecting to mysql');
$dbname = 'mydb';
mysql_select_db($dbname);
$uname = $_POST['login'];
$pword = $_POST['lpassword'];
$query = 'SELECT id FROM user WHERE username="$uname"';
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
echo <br>$num_rows</br>;

I just get the following returned in my browser:

$num_rows
; ?>

(Note; The $uname posted in the form is in the user table 5 times)

And, if I change the code to :
$dbhost = 'localhost';
$dbuser = 'myuser';
$dbpss = 'myassword';
//connect to database//
$conn = mysql_connect($dbhost, $dbuser, $dbpss) or die('Error connecting to mysql');
$dbname = 'mydb';
mysql_select_db($dbname);
$uname = $_POST['login'];
$pword = $_POST['lpassword'];
$query = 'SELECT id FROM user WHERE username="$uname"';
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
echo $num_rows;

my browser return is blank

I have run the following command from the Query Browser tool:
SELECT if FROM user WHERE username="martin.thorburn@tiscali.co.uk";

I have done this to ensure I am connecting to the DB correctly, and using the correct DB. I receive 5 results ($uname is martin.thorburn@tiscali.co.uk as entered in my form).

I have also tried different variations of echo, print and die (with error) but the browser return is blank.

I would be grateful if anyone can point me in the right direction?

Edited by nav33n: Use [code] tags for easy readability.

2
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by Thorby68
0
$query = 'SELECT id FROM user WHERE username="$uname"';

Since you are using single quotes for this string, $uname is not replaced with the value of your variable. You should use this:

$query = "SELECT id FROM user WHERE username='$uname'";

Strings with double quotes are parsed (for variable substitution), single quoted strings are not. See this: http://www.php.net/manual/en/language.types.string.php

Secondly, this:

echo <br>$num_rows</br>;

is missing quotes:

echo "<br/>$num_rows<br/>";

Edited by pritaeas: n/a

0

Thanks pritaeas,

Unfortunately, after making the changes, my browser results are still blank.

Therefore I've gone back to basis and I think I've established either my form or PHP variable are not working.

My Form (with page format removed):

<form name="form1" id="form1" method="post" enctype="application/x-www-form-urlencoded" action="../PHP/login.php">
<p>Login</p>
        <label for="textfield">Email address</label>   
          <input type="text" name="login" id="login" />
         <label for="textfield">Password</label>  
          <input type="text" name="lpassword" id="lpassword" />
           <label for="Submit"></label>
        <input type="submit" name="Submit" value="Submit" id="Submit" />
</form>

//My PHP
$uname = $_POST['login'];
$pword = $_POST['lpassword'];
echo <br/>$uname<br/>;

The resulting browser is still blank........

Have you come accross this before?

Edited by nav33n: Use [code] tags.

0

appologies PHP hould read:

$uname = $_POST['login'];
$pword = $_POST['lpassword'];
echo "<br/>$uname<br/>";

As you previously mentioned

Edited by nav33n: Use [code] tags.

0

It may be too obvious but I have to ask: You do have php tags around statements ?

<?php
  $uname = $_POST['login'];
  $pword = $_POST['lpassword'];
  echo "<br/>$uname<br/>";
?>

Edited by pritaeas: n/a

0

No problem,

Yes I start and end with PHP tags, I think this is why this is so wierd..

0

I now see that the action attribute of the form points to ../PHP/login.php

Can you try just login.php (assuming this is the name of your script).

If that doesn't work, try GET just for debugging.

Edited by pritaeas: n/a

0

Pritaeas, many thanks for your help.

I tried a great many things to rectify the problem, all with the same result. In the end I re-installed Apache, applied my saved config and this seems to have fixed whatever was causing the problem.

thanks for your assistance.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.