i got something really stange going on with mysql.look

<?php
$numid=$_POST['numid'];
$query1="select * FROM datatable LEFT JOIN datatable2 ON datatable.numid=datatable2.numid  WHERE ";
$connect=mysql_connect("xxx","xxx","xxx")
or die ("Check your server connection");
$db_found = mysql_select_db("mydb",$connect);
if ($db_found) {
    echo "Database FOUND <br>";

    if (strlen($numid)>0){
                   $query1.="numid='$numid' AND ";
    }
   
    $query1=substr($query1,0,(strlen($query1)-4));
    $result = mysql_query($query1);
    $num_r=mysql_num_rows($result);
    if ($num_r!=0){
           echo "FOUND ".$num_r." RESULTS<br><br>";
           while($nt=mysql_fetch_array($result)){
                 echo "surname: $nt[surname]<br>"; 
                 echo "name:$nt[name]<br>";
                 echo "ID:$nt[numid]<br>";  
           }
    }else {echo "no match found!";}
}
?>

and when i input a number in numid_field i got the message
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\... whats wrong ?

Recommended Answers

All 6 Replies

This means your mysql_query returns a false because there's an error in your query.

Which is the result of line 11 because your query will look like:
select * FROM datatable LEFT JOIN datatable2 ON datatable.numid=datatable2.numid WHERE numid='$numid' AND
There is nothing following the AND which produces an error.

This means your mysql_query returns a false because there's an error in your query.

Which is the result of line 11 because your query will look like:
select * FROM datatable LEFT JOIN datatable2 ON datatable.numid=datatable2.numid WHERE numid='$numid' AND
There is nothing following the AND which produces an error.

# i use the
$query1=substr($query1,0,(strlen($query1)-4));
isnt that make the query look likeselect * FROM datatable LEFT JOIN datatable2 ON datatable.numid=datatable2.numid WHERE numid='$numid'

with an echo i gt this
select * FROM datatable LEFT JOIN datatable2 ON datatable.numid=datatable2.numid WHERE numid='0010'
maybe i need a transrofmation of '$numid' with (int)'$numid'

Ok I'm sorry I missed that.
But there's still an error in your MySQL query and you would've known had you executed it like this:

$result = mysql_query($query1) or die(mysql_error());

The problem is that since both your datatable and datatable2 contain a field numid, the WHERE numid='0010' is ambiguous as you don't specify if it's the numid from the first or second table.
Even though your query suggests that the results wouldn't differ I think this is the problem.

no,i changed name of datatable2 and still getting the error.i tried now with error report and i get
Column 'numid' in where clause is ambiguous!how can this happened?since i changed name of datatable2 to id ???

sorry i drunk 2 beers and i didnt see that! its ambigous ~ i tried datatable.numid and work !!! tnx for replying m8 !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.