Jump to content

How to sort data mysql in php syntax

veera007

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\techponya\index.php on line x

 

 

<?php 
$sql2 = "SELECT * FROM artikel WHERE kategori='technews' DESC() LIMIT 5";
$hasil2 = mysql_query($sql2,$dbconn);
while ($techponya = mysql_fetch_array($hasil2)){ 
?>

 

Link to comment
Share on other sites

Link to post
Share on other sites

You have an sql error. Use mysql_error() to find out what error it is.

 

$sql2 = "SELECT * FROM artikel WHERE kategori='technews' DESC() LIMIT 5";
$hasil2 = mysql_query($sql2,$dbconn);

if(!$hasil2)
{
    echo "SQL Error: " . mysql_error();
}
else
{
    while ($techponya = mysql_fetch_array($hasil2))
    {
    	//... process results
    }
}

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

You must first connect to the SQL server, you get that error because there's no connection created.

You also need to use mysqli_ functions, because the plain mysql_ functions are outdated, not recommended to be used.

They kept the names almost the same for all functions.

 

mysqli may need to be uncommented in php.ini in order to be enabled (remove the ; in front and restart apache to get php to reload)

 

So it would be something like this :

 

//  host, user, password , database
$connection = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

/* Optional but highly recommended : Set the desired charset after establishing a connection 
 Should match the character set used when creating the tables
*/
mysqli_set_charset($connection, 'utf8mb4');

$parameter = 'technews'; 

// old style queries 

// any user submitted data or string must be escaped before adding to a sql query
// the function below takes in account the charset selected before to escape the string properly 
$parameter_escaped = mysqli_real_escape_string($connection,$parameter);

$query_text = 'SELECT * FROM artikel WHERE kategori=\''. $parameter_escaped.'\' ORDER BY id DESC() LIMIT 5';
$query_result = mysqli_query($connection, $query_text);
// now use mysqli_fetch_row or mysqli_fetch_array (numeric keys, associative keys, both numeric and assoc) or mysqli_fetch_assoc or mysqli_fetch_object to retrieve records
while ($result = mysqli_fetch_array($query_result)) {
  // do something with data retrieved
}

// -----------
// "modern style" using prepared statements, the myqli functions will escape the parameters 

/* create a prepared statement */
// ? will be replaced by our variable when we bind parameter - bind parameter function will also escape variable and put the quotes if needed in the string
$statement = mysqli_prepare($connection, "SELECT * FROM artikel WHERE kategori=? ORDER BY id DESC() LIMIT 5");

/* bind parameters - parameters are statement, type, variable  type can be 
    i represents an integer type
    d represents an double type
    s represents an string type
    b represents an blob type
*/
mysqli_stmt_bind_param($statement, "s", $parameter);

/* execute query */
$success = mysqli_stmt_execute($statement);
/* retrive all results */
$query_result = mysqli_stmt_get_result($statement);
/* do something with the results */
while ($result = mysqli_fetch_array($query_result)) {
  // do something with data retrieved
}

 

example of multiple bind :

 

//Inserting values into the table using prepared statement
   $id = 1;
   $fname = 'Smith';
   $lname = 'John';
   $pob = 'Vermont';
   $country = 'Canada';   
   
   $stmt = mysqli_prepare($connection, "INSERT INTO myplayers values(?, ?, ?, ?, ?)");

   //Binding values to the parameter markers
   mysqli_stmt_bind_param("issss", $id, $fname, $lname, $pob, $country);
   mysqli_stmt_execute($stmt);
   mysqli_stmt_close($stmt);
   

 

Edited by mariushm
added ORDER BY id to make the SQL queries correct
Link to comment
Share on other sites

Link to post
Share on other sites

I could be wrong (been a while with working with SQL), but in mySQL isn't sorting done liek the following

SELECT *
FROM table
ORDERBY column DESC;

Which would put your query as

$sql2 = "SELECT * FROM artikel WHERE kategori='technews' ORDERBY kategori DESC LIMIT 5";

I could be wrong, again it's been a few years since I did any real database query stuff (I'm probably in need of a refresher)

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, wanderingfool2 said:

I could be wrong (been a while with working with SQL), but in mySQL isn't sorting done liek the following

SELECT *
FROM table
ORDERBY column DESC;

Almost correct, it's "ORDER BY", not "ORDERBY". But yes, you need to specify by which column(s) you want to order.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×