Jump to content

mysql_num_row doesn't work


mpsn

Recommended Posts

Hi, I am trying to get the previous id for the table xdocument, but browser outputs this warning:

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given

 

for this code:

<?php
//HELPER FCN used by FCN: writeXMLtoDBviaDOM
function mysql_next_id($table) {
    $result = mysql_query('SHOW TABLE STATUS LIKE "'.$table.'"');
    $rows = mysql_fetch_assoc($result);
    return $rows['Auto_increment'];
}

//TEST RUN:
$_filePath="C:\dir\email.xml";
$node=basename($_filePath);
$dom=new DOMDocument();
$dom->load($node);
$labelPath=array();
mysql_connect("localhost","root");
mysql_select_db("dummydpev");
$isXdocExist=mysql_query("SELECT (file_Path,file_Name) FROM xdocument WHERE file_Path='$_filePath' AND file_Name='$node'");
$docId=0;

if(mysql_num_rows($isXdocExist)==0) {
mysql_query("INSERT INTO xdocument (file_Path,file_Name) VALUES ('$_filePath','$node')");
$docId=mysql_next_id("xdocument")-1;
}
else
$docId=mysql_next_id("xdocument")-1;

writeXMLtoDBViaDOM($dom->documentElement,$labelPath,$docId,$_filePath);
?>

So I think my problem is with the multiple WHERE clause conditions, can I do it like what I have above?

 

Any help much appreciated!

Link to comment
Share on other sites

Why does it still keep adding the SAME entry when I refresh the page? I though the condition below

$isXdocExist=mysql_query("SELECT (file_Path,file_Name) FROM xdocument WHERE file_Path='$_filePath' AND file_Name='$node'");
$docId=0;

if(mysql_num_rows($isXdocExist)==0) {
   //INSERT INTO table xdocument

will ensure if I refresh browser, script SHOULDN'T re-enter same entry or is it b/c my query ($isXdocExist) is all condisered UNIQUE b/c of different primary keys?

 

Any help much appreciated!

Link to comment
Share on other sites

Ok, I tried to put in UNIQUE, but now it doesn't insert at all into db table.

 

<?php
mysql_connect("localhost","root");
mysql_select_db("dummydpev2");
//$isXdocExist=mysql_query("SELECT (file_Path,file_Name) FROM xdocument WHERE file_Path='$_filePath' AND file_Name='$node'");
$docId=0;
$title="cashier";
$name="bob";
$isToyEntryExist=mysql_query("SELECT (title,name) FROM toy WHERE title='$title'");

//INSERT entry if NOT in table 
if(mysql_num_rows($isToyEntryExist)==0) {
mysql_query("INSERT INTO toy (title,name) UNIQUE VALUES ('$title','$name')");
}
else
print "Already in db dummpdpev2.toy!";

?>

 

Please help!

Link to comment
Share on other sites

This looks like it should work.

<?php

mysql_connect("localhost","root");
mysql_select_db("dummydpev2");
//$isXdocExist=mysql_query("SELECT (file_Path,file_Name) FROM xdocument WHERE file_Path='$_filePath' AND file_Name='$node'");
$docId=0;
$title="cashier";
$name="bob";
$isToyEntryExist=mysql_query("SELECT title,name FROM toy WHERE title='$title'");

//INSERT entry if NOT in table 
if(mysql_num_rows($isToyEntryExist)==0) {
mysql_query("INSERT INTO toy (title,name) VALUES('$title','$name')");
}
else
print "Already in db dummpdpev2.toy!";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.