Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\x

Discussion in 'PHP' started by exzevia, Oct 22, 2010.

  1. #1
    please help me i am new to PHP and i am having this error on my program

    (Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\project\viewforum.php on line 29)

    <?php
    include("config.php");
    if(isset($_GET['id']) == TRUE) {
    if(is_numeric($_GET['id']) == FALSE) {
    header("Location: " . $config_basedir);
    }
    $validforum = $_GET['id'];
    }
    else {
    header("Location: " . $config_basedir);
    }
    require("header.php");
    $forumsql = "SELECT * FROM forums WHERE id = " . $validforum . ";";
    $forumresult = mysql_query($forumsql);
    $forumrow = mysql_fetch_assoc($forumresult);
    echo "<h2>" . $forumrow['name'] . "</h2>";
    echo "<a href='index.php'>" . $config_forumsname
    . " forums</a><br /><br />";
    echo "[<a href='newtopic.php?id=" . $validforum . "'>New Topic</a>]";
    echo "<br /><br />";

    $topicsql = "SELECT MAX( messages.date ) AS maxdate,
    topics.id AS topicid, topics.*, users.*
    FROM messages, topics, users WHERE messages.topic_id
    = topics.id AND topics.user_id = users.id AND
    topics.forum_id = " . $validforum . " GROUP BY
    messages.topic_id ORDER BY maxdate DESC;";
    $topicresult = mysql_query($topicsql);
    $topicsnumrows = mysql_num_rows($topicresult);
    if($topicsnumrows == 0)
    {
    echo "<table width='300px'><tr><td>No topics!</td></tr></table>";
    }
    else {
    echo "<table class='forums'>";
    echo "<tr>";
    echo "<th>Topic</th>";
    echo "<th>Replies</th>";
    echo "<th>Author</th>";
    echo "<th>Date Posted</th>";
    echo "</tr>";
    while($topicrow = mysql_fetch_assoc($topicresult))
    {
    $msgsql = "SELECT id FROM messages WHERE
    topic_id = " . $topicrow['topicid'];
    $msgresult = mysql_query($msgsql);
    $msgnumrows = mysql_num_rows($msgresult);
    echo "<tr>";
    echo "<td>";
    echo "<strong>
    <a href='viewmessages.php?id="
    . $topicrow['topicid'] . "'>"
    . $topicrow['subject'] . "</a></strong></td>";
    echo "<td>" . $msgnumrows . "</td>";
    echo "<td>" . $topicrow['username'] . "</td>";
    echo "<td>" . date("D jS F Y g.iA", strtotime($topicrow['date']))
    . "</td>";
    echo "<tr>";
    }
    echo "</table>";
    ?>
    <?php
    }
    require("footer.php");
    ?>
     
    exzevia, Oct 22, 2010 IP
  2. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #2
    anything in


    $topicsql = "SELECT MAX( messages.date ) AS maxdate,
    topics.id AS topicid, topics.*, users.*
    FROM messages, topics, users WHERE messages.topic_id
    = topics.id AND topics.user_id = users.id AND
    topics.forum_id = " . $validforum . " GROUP BY
    messages.topic_id ORDER BY maxdate DESC;";


    that is varchar should be wrapped in single quotes

    then retry..


    also
    That error usually indicates that MySQL was unable to process the SQL you provided it, so you need to debug the query code. Check to see if the result of your mysql_query() is false, and if so, do an error_log() or otherwise output the query string and the result of mysql_error() to find out what was wrong with the query.
     
    lowridertj, Oct 22, 2010 IP
  3. flight05

    flight05 Active Member

    Messages:
    122
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #3
    I hate this error
     
    flight05, Oct 23, 2010 IP
  4. lukefowell89

    lukefowell89 Peon

    Messages:
    182
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This really is the mother of all annoying mySQL errors, its either the field count, something to do with quote marks, spelling mistake or nothing in your database.
     
    lukefowell89, Oct 25, 2010 IP
  5. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #5


    do as I suggested above to debug your situation. It will get it working properly.
     
    lowridertj, Oct 25, 2010 IP
  6. Sparaker

    Sparaker Peon

    Messages:
    204
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    mysql_query is probably returning a false due to query failure (because of possible incorrect SQL query). I would recommend fixing the query.
     
    Sparaker, Oct 26, 2010 IP
  7. exzevia

    exzevia Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\Forum\Login.php on line 64

    PLease help me with this error . here is the code:

    <?php
    session_start();
    require("config.php");
    require("functions.php");
    $db = mysql_connect($dbhost, $dbuser, $dbpassword);
    mysql_select_db($dbdatabase, $db);
    $db = mysql_connect($dbhost, $dbuser, $dbpassword);
    mysql_select_db($dbdatabase, $db);
    if($_POST['submit']) {
    $sql = "SELECT * FROM users WHERE username = '"
    . $_POST['username'] . "' AND password = '"
    . $_POST['password'] . "';";
    $result = mysql_query($sql);
    $numrows = mysql_num_rows($result);
    $result = mysql_query($sql);
    $numrows = mysql_num_rows($result);
    if($numrows == 1) {
    $row = mysql_fetch_assoc($result);
    if($row['active'] == 1) {
    session_register("USERNAME");
    session_register("USERID");
    $_SESSION['USERNAME'] = $row['username'];
    $_SESSION['USERID'] = $row['id'];
    switch($_GET['ref']) {
    case "newpost":
    if(isset($_GET['id']) == FALSE) {
    header("Location: " . $config_basedir .
    "/newtopic.php");
    }
    else {
    header("Location: " . $config_basedir .
    "/newtopic.php?id=" . $_GET['id']);
    }
    break;
    case "reply":
    if(isset($_GET['id']) == FALSE) {
    header("Location: " . $config_basedir .
    "/newtopic.php");
    }
    else {
    header("Location: " . $config_basedir .
    "/newtopic.php?id=" . $_GET['id']);
    }
    break;
    default:
    header("Location: " . $config_basedir);
    break;
    }
    }
    else {
    require("header.php");
    echo "This account is not verified yet. You were emailed a link
    to verify the account. Please click on the link in the email to
    continue.";
    }
    echo "This account is not verified yet. You were emailed a link
    to verify the account. Please click on the link in the email to
    continue.";
    }
    }
    else {
    header("Location: " . $config_basedir . "/login.php?error=1");
    }
    else {
    require("header.php");
    if($_GET['error']) {
    /echo "Incorrect login, please try again!";
    }
    ?>
    <form action="<?php echo pf_script_with_get($SCRIPT_NAME); ?>"
    method="post">
    <table>
    <tr>
    <td>Username</td>
    <td><input type="text" name="username"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" name="submit" value="Login!"></td>
    </tr>
    </table>
    </form>
    Don't have an account? Go and <a href="register.php">Register</a>!
    <?php
    }
    require("footer.php");
    ?>
     
    exzevia, Nov 2, 2010 IP
  8. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #8
    Should really start indenting your code more. helps when debugging like this.

    having it all railed to one side makes it a lot harder.

    Try this out.
    
    
    <?php
    session_start();
    require("config.php");
    require("functions.php");
    $db = mysql_connect($dbhost, $dbuser, $dbpassword);
    mysql_select_db($dbdatabase, $db);
    $db = mysql_connect($dbhost, $dbuser, $dbpassword);
    mysql_select_db($dbdatabase, $db);
    
    if($_POST['submit']) {
    $sql = "SELECT * FROM users WHERE username = '"
    . $_POST['username'] . "' AND password = '"
    . $_POST['password'] . "';";
    $result = mysql_query($sql);
    $numrows = mysql_num_rows($result);
    $result = mysql_query($sql);
    $numrows = mysql_num_rows($result);
    
    if($numrows == 1) {
    $row = mysql_fetch_assoc($result);
    
    if($row['active'] == 1) {
    session_register("USERNAME");
    session_register("USERID");
    $_SESSION['USERNAME'] = $row['username'];
    $_SESSION['USERID'] = $row['id'];
    
    switch($_GET['ref']) {
    case "newpost":
    
    if(isset($_GET['id']) == FALSE) {
    header("Location: " . $config_basedir .
    "/newtopic.php");
    }
    
    else {
    header("Location: " . $config_basedir .
    "/newtopic.php?id=" . $_GET['id']);
    }
    
    break;
    case "reply":
    if(isset($_GET['id']) == FALSE) {
    header("Location: " . $config_basedir .
    "/newtopic.php");
    }
    
    else {
    header("Location: " . $config_basedir .
    "/newtopic.php?id=" . $_GET['id']);
    }
    
    break;
    default:
    header("Location: " . $config_basedir);
    break;
    }
    
    
    else {
    require("header.php");
    echo "This account is not verified yet. You were emailed a link
    to verify the account. Please click on the link in the email to
    continue.";
    }
    }
    }
    
    echo "This account is not verified yet. You were emailed a link
    to verify the account. Please click on the link in the email to
    continue.";
    }
    
    else {
    header("Location: " . $config_basedir . "/login.php?error=1");
    }
    
    else {
    require("header.php");
    
    if($_GET['error']) {
    echo "Incorrect login, please try again!"; //had to remove a "/" in very front on this line
    }
    ?>
    <form action="<?php echo pf_script_with_get($SCRIPT_NAME); ?>"
    method="post">
    <table>
    <tr>
    <td>Username</td>
    <td><input type="text" name="username"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" name="submit" value="Login!"></td>
    </tr>
    </table>
    </form>
    Don't have an account? Go and <a href="register.php">Register</a>!
    <?php
    }
    require("footer.php");
    ?> 
    
    
    PHP:
     
    lowridertj, Nov 2, 2010 IP
  9. exzevia

    exzevia Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    its nw giving me this error :parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\Forum\Login.php on line 60
     
    exzevia, Nov 2, 2010 IP
  10. ExTeNsIoN

    ExTeNsIoN Member

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #10
    mostly cuz of wrong type of parameters. this is such an annoying error
     
    ExTeNsIoN, Nov 3, 2010 IP
  11. Hello 2000

    Hello 2000 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #11
    Fix my code, please!

    Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\


    index.php
    <?php
    include 'includes/connection.php';

    $query = "SELECT * FROM tuotteet";

    $result = mysql_query($query);

    while($tuote = mysql_fetch_array($result)) {
    echo "<h3>" . $tuote['Nimi'] . "</h3>";
    echo "<p>" . $tuote['Kuvaus'] . "</p>";
    echo "<a href=\"modify.php\id=" . $tuote['ID'] . "\">Muokkaa Tuotetta</a>";
    echo "<span> </span>";
    echo "<a href=\"delete.php?id=" . $tuote['ID'] . "\">Muokkaa Tuotetta</a>";
    }
    ?>
    <h1>Lisää tuote</h1>
    <form action="create.php" method="post">
    Tuotteen nimi
    <input type="text" name="inputName" value="" /><br />
    Tuotteen kuvaus
    <input type="text" name="inputDesc" value="" />
    <br />
    <input name="submit" type="submit" id="submit" />
    </form>
     
    Hello 2000, Feb 3, 2014 IP
  12. Hello 2000

    Hello 2000 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #12
    Line 8
     
    Hello 2000, Feb 3, 2014 IP