I'm having a problem running a sql query using php.
$sql = "SELECT * FROM ".self::$table_name;
$result = mysql_query( $sql );
$r = mysql_fetch_array( $result );
print_r( $r );
die( '<br>'.$sql );
I have around 70 records in table but i'm only getting the first record.
see example.
Array ( [0] => site_url [setting_name] => site_url [1] => http://domain.com [value] => http://domain.com )
SELECT * FROM siteconfig
When I run the query in phpmyadmin. it works fine.
You have to make a loop to grab all the results:
$r = array();
while($junk = mysql_fetch_array($result)) $r[] = $junk;
print_r($r);
Do it like below:
$sql = "SELECT * FROM ".self::$table_name;
$result = mysql_query( $sql );
while($r = mysql_fetch_array($result)){
echo $r['col1']. " - ". $r['col2'];
// your stuff
}
Related
i have this code :
$result = mysqli_query($conn, "SELECT SUM(angsuran) FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
print_r($row);
when i run this code, the result is :
Array ( [SUM(angsuran)] => 30000 )
But i want it to display just "30000" number, without "Array ( [SUM(angsuran)] => ) ".
How can i do that ?
add alias in your query like
$result = mysqli_query($conn, "SELECT SUM(angsuran) as sumtotal FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
echo $row['sumtotal']; //outputs 30000
I have 2 ways
Use array php
$result = mysqli_query($conn, "SELECT SUM(angsuran) FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
echo $row["SUM(angsuran)"]; // out 30000
Modify sql
$result = mysqli_query($conn, "SELECT SUM(angsuran) as sum FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
echo $row["sum"]; // out 30000
I am trying to Query a MySQL table to to bring any result that matches data that the user has input. The database,table and column names are also dynamically stored in variables. var_dump produces a bool(false) which means my query is wrong.
My Code
if (isset ( $_POST ['name'] )) {
$name = trim ( $_POST ['name'] );
$tblName = $_REQUEST ['tbl'];
$colqry = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$dbName' AND TABLE_NAME = '$tblName'";
echo "<ul>";
$col_names = mysqli_query ( $link, $colqry );
while ( $col = mysqli_fetch_array ( $col_names, MYSQL_ASSOC ) ) {
$colName = $col ['COLUMN_NAME'];
$tblQry = "SELECT * FROM $tblName WHERE $colName=$name LIMIT 10";
$query2 = mysqli_query ($link, $tblQry);
echo $query2;
while ( $query3 = mysqli_fetch_array ( $query2 ) ) {
echo "<li onclick=fill'" . $query3 [0] . "'>" . $query3 [0] . "</li>";
}
}
}
What I want to achieve is list a table where the search terms matches something on the table either the column name or the data inside the columns
This line:
$tblQry = "SELECT * FROM $tblName WHERE $colName=$name LIMIT 10";
Quote the $name variable:
so it reads as WHERE $colName='$name'
You can then use $query3[$colname] to get the search match you're looking for.
For more information on identifer qualifiers, visit:
http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html
I have the table name in the form as a hidden field, i have to do it this way for some reason but it's not working when i do the following:
// This is being processed from the previous page in a form
$table = mysql_real_escape_string($_POST['just_field']);
//Here i am using the $table value to use in place of a table name
$sql = "select `Field` from `$table` where `another_field` = '$id'
and `another_another_field` = '$something'";
$query = mysql_query($sql) or die ("Error: ".mysql_error());
while ($row = mysql_fetch_array($query)){
$somefield = $row['field'];
}
mysql_free_result($query);
How can i make it work so the $table is being read by the command...
No need for quotes around the table assignment.
$table = mysql_real_escape_string($_POST['just_field']);
try to include the connection string
$sql = "select `Field` from `$table` where `another_field` = '$id'
and `another_another_field` = '$something'";
$query = mysql_query($sql,$conn) or die ("Error: ".mysql_error());
while ($row = mysql_fetch_array($query)){
$somefield = $row['field'];
}
mysql_free_result($query);
if you are not using any framework
Does this work for you
$sql = "select `Field` from ".$table." where `another_field` = '$id'
and `another_another_field` = '$something'";
Your problem is case-sensitivity.
In your query:
select `Field` from `$table`....
^ Capital F
And the later
$somefield = $row['field'];
^ lower case f
An example
php > $cxn = mysql_connect( 'localhost', '****', '****' );
php > mysql_select_db( 'test' );
php > $res = mysql_query( 'select * from genres' );
php > $row = mysql_fetch_assoc( $res );
php > print_r( $row );
Array
(
[id] => 1
[name] => Pop //notice the field name is lower case
)
php > echo $row['name']; //this works
Pop // <- see
php > echo $row['Name']; //this doesn't
php > // <- look, nothing
Try printing the contents of $table before sending it through mysql_real_escape_string() to make sure it contains the value you think it does.
I have this code
if(!isset($_GET['album_id'])) { die("Album Not Found!"); } else { $album_id = mysql_real_escape_string($_GET['album_id']); }
$sql = "SELECT * FROM `audio_albums` WHERE `album_id` = ".$album_id."";
$qry = mysql_query($sql);
$num = mysql_num_rows($qry);
if($num == 1) {
// Fetch Array
$arr = mysql_fetch_array($qry);
// Assign Values
$album_name = $arr['album_name'];
$album_name_seo = $arr['album_name_seo'];
$album_id = $arr['album_id'];
// Fetch Songs
$sql2 = "SELECT audio_id,album_id,title FROM `audios` WHERE `album_id` = ".$album_id." AND `public_private` = 'public' AND `approved` = 'yes' LIMIT 0, 30 ";
$qry2 = mysql_query($sql2);
$arr2 = mysql_fetch_array($qry2);
print_r($arr2);
} else {
echo "Album Not Found!";
}
and when i execute the code, it results in this
Array
(
[0] => qCpPdBZIpkXfVIg4iUle.mp3
[audio_id] => qCpPdBZIpkXfVIg4iUle.mp3
[1] => 1
[album_id] => 1
[2] => Ambitionz Az a Ridah
[title] => Ambitionz Az a Ridah
)
Actually it fetches data of only one row but there are several rows in result. Whats wrong in the code? why isn't it working?
Well, mysql_fetch_array fetches one row. You just need to do a loop to fetch all of them, as the manual shows: http://php.net/mysql_fetch_array
while ($arr2 = mysql_fetch_array($qry2)) {
print_r($arr2);
}
You really should learn some SQL ( yes , actually learn it ), and stop using the horribly outdated mysql_* functions.
$stmt = $dbh->prepare('
SELECT
audios.audio_id AS audio_id
audio_albums.album_id AS album_id
audios.title AS title
audio_albums.album_name AS album
audio_albums.album_name_seo AS seo_album
FROM audios
LEFT JOIN audio_albums USING (album_id)
WHERE
audio_albums.album_id = :id
audios.public_private = "public" AND
audios.approved = "yes"
LIMIT 0, 30
');
$stmt->bindParam( ':id', $_GET['album_id'], PDO::PARAM_INT );
if ( $stmt->execute() )
{
var_dump( $stmt->fetchAll(PDO::FETCH_ASSOC) );
}else{
echo 'empty .. try another';
}
Hello I have the following php script
$db =& JFactory::getDBO();
$sql = "select uid from #__zcalendar_events where article_id = " . $article->id;
$db->setQuery($sql);
$results = $db->loadObjectList();
if(count($results)) {
foreach($results as $r => $value) {
$sql = "select catid from #__zcalendar_event_categories where uid = " . $r->field1;
print_r($results);
print_r($sql);
My problem start in this line:
$sql = "select catid from #__zcalendar_event_categories where uid = " . $r->field1;
the . $r->field1; does not add the value to the string $sql.
But if I print_r($results); I get the this information:
Array ( [0] => stdClass Object ( [uid] => 2011-08-22-12-24-35-62-0#cttcorp.hexasystems.com ) )
And I need just this: 2011-08-22-12-24-35-62-0#cttcorp.hexasystems.com
and . $r->field1; have to had this information but it's always empty.
Can someone please help me to solve this problem, because I have the information. But never works.
A foreach is usually like this:
foreach($array as $key => $element)
Are you sure you want to be using the $r not the $value?