Jump to content

Calling a specific row in Sql when ever i like


rmariano02

Recommended Posts

How can i call a specific row when ever a like.

 

example :

 

$record = mysql_query("select * from table_name");
($getValue= mysql_fetch_row($record)   //i'm not sure what should i use. should i use mysql_fetch_array, mysql_ fetch_ assoc, mysql_ fetch_ object,  mysql_ field_ name and etc.

print $getValue[$column_name][0];

 

0 = value 1st row in the database/table_name

1 = value 2nd row in the database/table_name

2 = value 3rd row in the database/table_name

......

....

......

.....

etc.

 

is there a code i can do except from using sql_fetch_array then loop and put in into a multidimensional array?

Link to comment
Share on other sites

You'd have to use MySQLi

 

<?php

$sql = new MySQLi('localhost','root','pass','db');

$query = 'SELECT * FROM table';
$result = $sql->query($query);
if( $result == FALSE )
echo 'There was a query error';
else
$data = $result->fetch_all(MYSQLI_ASSOC);

print_r($data);

?>

 

Or code the loop yourself

 

<?php

mysql_connect('localhost','root','');
mysql_select_db('db');

$query = 'SELECT * FROM table_a';
$result = mysql_query($query);

if( $result == FALSE )
echo 'Query error';
else {
$data = array();
while( $row = mysql_fetch_assoc($result) ) $data[] = $row;
}

print_r($data);

?>

Link to comment
Share on other sites

Sorry i'm just new in PHP and SQL

 

can i know what do you mean by

Those snippets will work independent of each other.

 

If you use the MySQLi version, I see no reason to keep the rest of your application on the old, unmaintained MySQL functions.

 

sorry i'm not good in understanding English.

do you mean is that i can no logger use the calling of sql in to SQLi?

or i can still use the same code because SQLi can adopt to SQL old codes.

 

i'm realy sorry for your trouble  .  :-[

Link to comment
Share on other sites

No, MySQLi knows nothing about the old MySQL resources or functions.

 

I've provided you with a solution that works in both sets of functions.

 

I'd strongly suggest moving your entire script over to MySQLi, as the old MySQL functions are no longer actively developed. That said, your application will still work fine if you stick with the old.

 

If you don't want to change, the second snippet will do what you want.

Link to comment
Share on other sites

Never too many questions. As long as you are ready to learn, we're here to help :D

 

PHP keeps a lot of old functions that have been replaced or deemed 'bad practise' to avoid breaking existing, old scripts. This can make life VERY confusing for a new PHP developer, especially because a LOT of tutorials are never updated to reflect these changes.

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.