-1

Here's my code, I have a problem with while, because when I run it in WAMP, this happens:

Warning: Mysql_num_rows() expects parameter 1 to be resource

Warning: mysql_fetch_array() expects parameter 1 to be resource

(Full error).

    <?php
    mysql_select_db("spectrum-solaris",$conex);
    $query = mysql_query("SELECT id,name,tittle,body FROM articles ORDER BY id             DESC",$conex);
    $row = mysql_num_rows($query);
    if($row > 0 ){
    while($row = mysql_fetch_array($query))
    {
    ?>
    <div class ="tematica" >
    <p>
    <small>Published by <b><a href="user.php"><?= $row['name'] ?></a></b></small>
    </p>
    <p>
        <big><a href="show.php"><?= $row['tittle'] ?></a></big>
    </p>
    <p>
        <b><?=$row['body']?></b>
    </p>        
    </div>
    <?php
    }
    }
    mysql_free_result($query);
    mysql_close($conex);
6
  • Hi, can you clarify what the problem is? Commented Nov 29, 2013 at 18:37
  • 1
    You should really use MySQLi or PDO. The mysql_* functions are deprecated. Commented Nov 29, 2013 at 18:38
  • wats the error you are getting Commented Nov 29, 2013 at 18:38
  • "it" happens? THE it? the one and only one which is so famous that it needs no explanation? Commented Nov 29, 2013 at 18:39
  • -1 for asking about an API you shouldn't even be using. ext/mysql is deprecated, and has been for a while now. Stop using it, or stop expecting people to help you figure out how to use it. Commented Nov 29, 2013 at 18:42

3 Answers 3

1

Functions mysql_num_rows() and mysql_fetch_array() needs the result resource that is being evaluated. This result comes from a call to mysql_query().

So you need to modify your code. First then use mysql_num_rows and mysql_fetch_array after $query = '...' insert $result = mysql_query($query); and then you can run mysql_num_rows($result) and mysql_fetch_array($result).

Your errors happens cause data that you give to mysql_num_rows and mysql_fetch_array not resource type.

And i recommend you to start using PDO not mysql_* functions.

0

mysql_num_rows, and mysql_fetch_array expect a valid resource. This would normally come from mysql_query, as you have.

But your mysql_query is failing first, as you never connect to your database, in this code snippet at least.

See,

mysql_select_db("spectrum-solaris",$conex);

$conex should be a reference to mysql_connect()

Change,

<?php
    mysql_select_db("spectrum-solaris",$conex);

To,

<?php

$conex = mysql_connect('host', 'username', 'password');
mysql_select_db("spectrum-solaris",$conex);
-1

As you have not specified the error, I'll just have to have a stab:

This code will not parse as it is incorrect.

<b><?=$row['body']?></b>

should perhaps read something like:

<b><? echo($row['body']); ?></b>

perhaps. You have a couple of other similar errors in your page.

Two more things:

  1. Try hard not to mix PHP and HTML like this. Your code is not the worst offender on the site but it will get harder to debug as your scripts become more complex.

  2. cHao is bang on the money with mysql_ please use mysqli_ or PDO both of which have a number of advantages, not least of which is they are the current recommended forms. mysqli_ is no harder to use than mysql_ the only difference you will notice at your level is the requirement for a connection to be specified in some of the functions.

1
  • 1
    Your first statement isn't true; <?= $value ?> will echo that value - it's the <?= that does it. I always use echo with a long opening tag, though it really doesn't need to be wrapped in brackets. In my view, modern PHP code tends to use echo without them. Commented Nov 30, 2013 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.