Hi I'm starting out on php and I have the following script
<?php
$username = "firstleg_floa";
$password = "**";
$hostname = "***1";
$db = "firstleg_bank";
$guildUser = strtolower('grissiom');
$dbh = mysql_connect( $hostname, $username, $password) or die ("Unable To Connect");
$connectDB = mysql_select_db($db, $dbh);
$results = mysql_query("SELECT * FROM Bank where userId ='" .$guildUser."'");
$i = 0;
$rsArr = array();
While ($row = mysql_fetch_array($results, MYSQL_ASSOC))
{
$rsArr [$i] [0] = $row{'userId'};
$rsArr [$i] [1] = $row{'item'};
$rsArr [$i] [2] = $row{'amount'};
$rsArr [$i] [3] = $row{'position'};
$i++;
}
?>
<?="ghdfdgdfg ". $rsArr[$i][1];}." ----";?>
<? echo $rsArr[$i][0]; ?>
<table>
<tr><td>Item</td><td>Amount</td></tr>
<?for ($x=0;$x <= $i; $x++)
{?>
<tr><td><?=$rsArr[$x][3];?></td><td><?=$rsArr[$x][2];?></td></tr>
<?}?>
</table>
<?php
mysql_close($dbh); ?>
and I have the following data in the database
1 - grissiom - Silk Cloth - 100 - 1
with the above data and the above script all I can manage to pull out is the amount(100) and the position(1) how come i am unable to pull anything else out? can someone help me please
It could be because you're using curly braces instead of square. The row returned by mysql_fetch_array() is a PHP array, not an object or string.
Possible solutions:
1) Those column names in the DB have different case, e.g. userId could be just userid
2) Those columns don't exist.
3) You are using curly braces, instead of square brackets:
$rsArr [$i][0] = $row{'userId'}; //wrong
$rsArr [$i] [0] = $row['userId']; //correct
Try replacing the while loop with this, it will read out the contents of each record as the DB has passed it to PHP:
while ($row = mysql_fetch_array($results, MYSQL_ASSOC))
{
echo '<pre>' . var_dump($row) . </pre>;
}
to see exactly what is coming back from the DB.
after loop is finished $i is equal to 1, but $rsArr[$i] is not defined, so variables $rsArr[$i][0] and $rsArr[$i][1] doensn't exists, try using $rsArr[0][0] and $rsArr[0][1] like this:
<?="ghdfdgdfg ". $rsArr[0][1];}." ----";?>
<? echo $rsArr[0][0]; ?>
<table>
<tr><td>Item</td><td>Amount</td></tr>
<?for ($x=0;$x <= $i; $x++)
{?>
<tr><td><?=$rsArr[$x][3];?></td><td><?=$rsArr[$x][2];?></td></tr>
<?}?>
</table>
It seem ok to me, maybe issue is given from using curly braces.
Anyway i'd go with some simpler syntax, i.e:
<?php
$username = "firstleg_floa";
$password = "**";
$hostname = "***1";
$db = "firstleg_bank";
$guildUser = strtolower('grissiom');
$dbh = mysql_connect( $hostname, $username, $password) or die ("Unable To Connect");
$connectDB = mysql_select_db($db, $dbh);
$results = mysql_query("SELECT * FROM Bank where userId ='" .$guildUser."'");
$i = 0;
$rsArr = array();
while ($row = mysql_fetch_array($results, MYSQL_ASSOC))
{
$rsArr[] = $row;
}
mysql_close($dbh);
echo '<table><tr><td>Item</td><td>Amount</td></tr>';
for ($i = 0;$i<count($rsArr);$i++){
echo <<<EOF
<tr><td>{$rsArr["item"]}</td><td>{$rsArr["amount"]}</td></tr>
EOF;
}
echo '</table>';
?>
Related
I am trying to fetch the values from mysql table based on the certain values. below is my php script where i am getting json values from android and then parse it to array the passing array in to the select query.
So, when i open the script in IE i am getting values as null instead of []. What is wrong here.
php
<?php
$username = "xxxxx";
$password = "xxxxxx";
$host = "localhost";
$database="xxxxxx";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$JSON_received = $_POST["JSON"];
$obj = json_decode($JSON_received,true);
foreach ($obj['ilist'] as $key => $value)
{
//echo "<br>------" . $key . " => " . $value;
$im[] = $value;
}
$myquery = "SELECT * FROM Movies WHERE im_rat IN ('$im')";
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
mysql_close($server);
?>
Can anyone help ?
I've tried for a couple of days to get all of the data from a MySQL column and put it inside an array, formatted in the following way:
$aSpam= array
( '.info'=> 'i'
, 'anal'=> 'i'
, 'anus'=> 'i'
, 'arse'=> 'i'
)
I've managed to echo it out formatted properly as you can see here: http://www.yourgrumble.com/phpbbforum/getSpam.php
with the following PHP code:
<?php
$servername = "localhost";
$username = "username";
$password = "pass";
$dbname = "db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT `SpamWord` FROM spamWords";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$counter = 0;
while($row = $result->fetch_assoc()) {
if($counter){
echo ", '" . $row["SpamWord"]. "'=> 'i'";
$counter++;
} else {
echo "'" . $row["SpamWord"]. "'=> 'i'";
$counter++;
}
}
} else {
echo "Error!";
}
$conn->close();
?>
I've read and tried more than 10 solutions found in the web and here at stackoverflow, however none of them worked. I've really got desperate and I cannot get through this without your help guys.
Edit
For example I tried with this solution, but it didn't work:
while ($row = mysql_fetch_array($result))
{
$new_array[$row['id']]['SpamWord'] = $row['SpamWord'];
}
foreach($new_array as $array)
{
echo $array['SpamWord'].'<br />';
}
Thank you all in advance,
Denis Saidov
Try like below:-
$sql = "SELECT `SpamWord` FROM spamWords";
$result = mysqli_query($conn ,$sql) or die(mysqli_error($conn));
$resultArray = array(); // create an array
if ($result->num_rows > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$resultArray[$row["SpamWord"]] = 'i'; // assing value
}
} else {
echo "Error!";
}
echo "<pre/>";print_r($resultArray); // print array
$conn->close();
Note: Here you will get your original array containing all SpamWord values comes from database.thanks
PHP array's are like dictionnaries (a list of key/value pairs)
First, before your loop, create your array empty:
$new_array = Array();
while...
Then for each column, you append the column value as a new key for the array
$new_array[$row['SpamWord']] = "i";
As in your example, I put "i" as the value for each array's row.
i'm a newbie to php still.
I'm using phpmyadmin as my database. I have a table called 'lessonno' and a column named 'lesson' in it. I tried using this code to retrieve out the number inside 'lesson'. But it's not printing out anything. Can someone help?
<?php
$server = 'localhost';
$username = '';
$password = '';
$database = 'project';
mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$sql = "SELECT 'lesson' FROM 'lessonno'";
$lesson = $_POST['lesson'];
$result = mysql_query($sql);
?>
<?php
for($i = 1; $i <= $lesson; $i++) {
echo "<div>
<span>Lesson ".$i."</span>
</div>
<br>";
}
?>
You can use something like this:
$sql = "SELECT lesson FROM lessonno";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo $row['lesson'];
}
If you would like to only print out a specific lesson with an certan ID, you can use something along the lines:
$id = $_GET['lessonid']; // If you would have something like index.php?lessonid=36 and you'd like it to only fetch the data for the lesson with the id of 36.
$sql = "SELECT lesson FROM lessonno WHERE id='$id'";
(by looking at the $_POST['lesson'] part, I suppose that's something you might be trying to do as it's in the for loop as well)
Also, I suggest you use mysqli.
And, this:
echo "<div>
<span>Lesson ".$i."</span>
</div>
<br>";
Will just echo the $i as both lesson= and the span with Lesson, which won't grab any information from the actual database but just go with the current number it's at, from the for loop you have.
i have made some changes in your code try this
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'project';
$conn = mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database, $conn) or die(mysql_error());
$sql = "SELECT `lesson` FROM `lessonno`";
$lesson = $_POST['lesson'];
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$lesson_no = $row['lesson'];
echo "<div>
<span>Lesson ".$lesson_no."</span>
</div>
<br>";
}
?>
Note : mysql_* is deprecated. use mysqli_* OR PDO
For getting Values from DB you need to use something like this
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
For further reference please visit http://in2.php.net/manual/en/function.mysql-fetch-assoc.php
For counting the number of data in your database, just insert this code
$sql = "SELECT 'lesson' FROM 'lessonno'";
$lesson = $_POST['lesson'];
$result = mysql_query($sql);
$count=mysql_num_rows($result);//this will count the number of rows in your table.
echo "<div>
<span>Lesson ".$count."</span>
</div>
<br>";
I have two rows in my MySQL data that I would like to have code echoed only if the MySQL row data is equal to '1' (as opposed to '0'). Here's the code so far, which seems to have some severe errors:
$query = "SELECT 162, 164 FROM search WHERE title = $title";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_row()) {
if ($row["162"] = 1) {
echo '<div id="162link">1.6.2</div>';
}
}
if ($row["164"] = 1) {
echo '<div id="162link">1.6.2</div>';
}
}
}
$result->close();
}
$mysqli->close();
As it says in the code above the two rows are "162" and "164" in the database.
Use:
if ($row["162"] == 1)
Instead of:
if ($row["162"] = 1)
and:
if ($row["164"] == 1)
I tried for you something like this if it gives you some idea:
$host = "localhost";
$user = "myusername";
$pass = "mypassword";
$database = "WorldEngine";
$mysqli = new mysqli($host, $user, $pass, $database);
$title = "My Good News";
$query = "SELECT `162`, `164` FROM search WHERE title = '$title';";
if ($result = $mysqli->query($query)) {
$i = 0;
while ($row = $result->fetch_row()) {
if ($row["162"] == 1) {
echo '<div id="162link' . $i . '">1.6.2</div>';
}
if ($row["164"] == 1) {
echo '<div id="164link' . $i . '">1.6.4</div>';
}
$i++;
}
$result->free();
}
$mysqli->close();
The index $i is appended to the div ID in order to produce unique DOM element ID's in the HTML document. I would also suggest you to change your numerical column names into alphabet-starting names like c162, c164, ...
Hope this will help you.
This question already has answers here:
How to get one column of mysql_query results into an array?
(4 answers)
Closed 1 year ago.
I have a table my_entity_data in that i have a column parentproduct_id
I want to get all values of that column in side one array
<?php
$result = mysql_query("SELECT parentproduct_id FROM my_entity_data");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row['parentproduct_id'];
}
for ($i=0; $i < 10; $i++) {
echo $storeArray[i];
}
?>
But no use Any thing wrong i did here ?
And i am running this code in Magento CE 1.7
Any ideas ?
I
Thanks to every one finally i got it
<?php
// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'DB Name';
$connection = mysql_connect($dbhost,$dbuser,$dbpass);
// Check connection
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db($dbname,$connection);
$result = mysql_query("SELECT parentproduct_id FROM my_entity_data");
$storeArray = array();
while ($row = mysql_fetch_array($result)) {
array_push($storeArray,$row['parentproduct_id']);
}
for ($i=0; $i < 10; $i++) {
echo $storeArray[i];
}
//echo sizeof($storeArray);
print_r($storeArray); //to see array data
?>
you can use
Try mysql_fetch_assoc($result);
Below note from: php.net
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $key => $value) {
$storeArray[$i][$key] = $value;
}
$i++;
}
for ($i=0; $i < 10; $i++) {
echo $storeArray[i];
}
hope this will sure work for you.
Try this
<?php
$result = mysql_query("SELECT parentproduct_id FROM my_entity_data");
$storeArray = array();
while ($row = mysql_fetch_array($result)) {
array_push($storeArray,$row['parentproduct_id']);
}
print_r($storeArray); //to see array data
?>