$sql = "SELECT `description`,`auction_id` FROM `auctions` WHERE `description` REGEXP '(97(8|9))?[[:digit:]]{9}([[:digit:]]|X)'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$row = mysql_fetch_row($result);
echo "$row[1]";
is there any reason why echo "$row[1]"; returns every other row? the results i am getting is suggesting as such, I am trying to get every row
To actually answer your question, why it is occurring
$row = mysql_fetch_assoc($result); //this advances the internal data pointer by 1
$row = mysql_fetch_row($result); //this advances it by 1 more
Both are being executed in every iteration of your loop.
You should use one, not both.
Additionally, you shouldn't be using mysql_* functions in new code, as it is deprecated. Use PDO or MySQLi as stated in the comments.
You are calling the fetch function two times and hence it appears to skip one row each time.
while ($row = mysql_fetch_assoc($result)) {
$row = mysql_fetch_row($result); //This is not needed
echo "$row[1]";
}
Should be
while ($row = mysql_fetch_assoc($result)) {
echo $row[1];
}
Instead of having the loop as:
while ($row = mysql_fetch_assoc($result)) {
$row = mysql_fetch_row($result);
use:
while ($row = mysql_fetch_assoc($result)) {
You should use like this.
while ($row = mysql_fetch_assoc($result)) {
$desc[] = $row['description'];
}
If the query work properly in phpmyadmin then either you can use
while ($row = mysql_fetch_assoc($result)) {
echo $row['description'];
}
Or
$row = mysql_fetch_row($result);
echo $row[1];
or
while ($row = mysql_fetch_array($result)) {
echo $row['description']; //or
echo $row[1];
}
Related
i want to convert my array into string i mean i want to echo only array value
i use this code
$query = "SELECT `message` FROM `appstickers`";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$jsonArray = array();
while($row = $result->fetch_assoc()) {
$jsonArray[]= $row;
}
//echo $string;
echo json_encode($jsonArray);
i get this output
[{"message":"welcome to team with us"},{"message":"this is for light dispatch"}]
but i want this output
[welcome to team with us,this is for light dispatch"]
so please give me proper solution with example
So why are you using json_encode() ?
use implode() instead:
// ... previous code
while($row = $result->fetch_assoc()) {
$jsonArray[]= $row;
}
echo '[' . implode(',', $jsonArray) . ']';
I'm going on a limb here, and assuming your required array is not what you actually want, but you do want this:
$jsonArray = array();
while($row = $result->fetch_assoc()) {
$jsonArray[]= $row['messsage'];
}
echo json_encode($jsonArray);
which will result in
["welcome to team with us", "this is for light dispatch"]
<?php
echo implode(" ",$jsonArray);
?>
Just you need to use implode() function.
$query = "SELECT `message` FROM `appstickers`";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$jsonArray = array();
while($row = $result->fetch_assoc()) {
$jsonArray[]= $row;
}
//echo $string;
$ans = implode(",",$jsonArray);
echo $ans;
I have this code...
$results_query = "SELECT * FROM profile WHERE string LIKE '%" . $search_deliminator . "%'";
$results_result = $database->query($results_query);
$result = mysql_fetch_array($results_result);
What I'm trying to do is pull all the rows in which the string column contains my search deliminator. Further, I would like to get the values from each column of the rows.
How would I pull the multidimensional array I need containing each row, and each value of each column within each row?
EDIT:
I'm looking to do something to this effect...
while ($row = mysql_fetch_assoc($results_result)) {
$result[] = $row;
}
Then echo each column like this...
foreach ($result as $row) {
echo $row["0"];
}
To get a flattened array of the result set, try this:
$result = array();
while ($row = mysql_fetch_assoc($results_result)) {
$result[$row['id']] = $row;
}
echo json_encode($result);
Also, you should look into either MySQLi or PDO to replace the (now deprecated) MySQL extension.
If you use PDO, you can use the fetchAll() method:
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
With mysql or mysqli, you use a loop:
$rows = array();
while ($row = $stmt->fetch_assoc()) {
$rows[] = $row;
}
try this
$i=0;
while ($row = mysql_fetch_assoc($results_result)) {
$result[$i]["column1"] = $row["column1"];
$result[$i]["column2"] = $row["column2"];
$i++;
}
To display the output use:
foreach ($result as $row) {
echo $row["column1"];
echo $row["column2"];
}
$query = "SELECT * FROM main";
if ($result = $db->query($query)) {
while ($row = $result->fetch_assoc()) {
$name= $row["name"];
$image = $row["image"];
}
}
then somewhere in my code I print it out using like echo $name; but I got only one result, which is the last row. I know I have to do foreach but what variable should I put?
foreach($result as $results) ? like this?
On every iteration, you reassign the values to $name and $image, causing it to show only the last value when it leaves the loop.
You can either echo them right away in the loop, or populate an array or an object with them, so they will be available later.
Example:
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = array('name'=>$row["name"], 'image'=>$row["image"]); // push into the array
}
var_dump($data); // it's all here now
And to echo the data later, one of the ways is foreach:
foreach($data as $row) {
echo "Name: ", $row['name'], "; Image: ", $row['image];
}
$query = "SELECT * FROM main";
$result = $db->query($query);
$row = $result->fetch_assoc();
foreach ($row as $rows) {
echo $rows['name'] ." ". $rows['image'];
}
I have the following code:
<?php
if(isset($_POST['indexSearchSubmit']))
{
foreach($_POST['industryList'] as $selected)
{
$_POST['industryList'] = $selected;
$locationListResults = $_POST['locationList'];
$results = mysqli_query($con,"SELECT * FROM currentListings WHERE location = '$locationListResults' AND industry = '$selected'");
while($row = mysqli_fetch_array($results))
{
echo $row['industry'];
echo $row['location'];
echo $row['title'];
echo $row['description'];
}
}
mysqli_close($con);
}
?>
Could anyone tell me how I would go about storing the echo part into a variable so I can then display it as and where I want in other parts of the site?
If I remove the echo and instead store $row as a variable when I echo that variable it only outputs once and doesn't run the loop.
You should use mysqli_fetch_all for this. The result will be an array where each element is a row and each row is an associative array with the same keys as row in your example
$data = mysqli_fetch_all($result);
$data[0]["industry"]; //Data in the first row
You can then loop over $data to output it any place on your page.
put in array,
$list = array();
while($row = mysqli_fetch_array($results))
{
$list[] = $row;
}
you may try this
$arrayList = array();
while($row = mysqli_featch_array($results)){
$arrayList[] = $row;
}
print_r($arrayList);
Put all the values in an array
$rows = array();
$x = 0;
while($row = mysqli_fetch_array($results))
{
$rows[$x]['industry'] = $row['industry'];
$rows[$x]['location'] = $row['location'];
$rows[$x]['title'] = $row['title'];
$rows[$x]['description'] = $row['description'];
$x++;
}
return $rows;
Then you can use $rows as an array.
foreach($rows as $v){
echo $v['industry']." ".$v['location']."<br />";
echo $v['title']." ".$v['description']."<br />";
}
i noticed that there are a lot of topics like this in stackoverflow, but the ones that i read are with just 1 variable, and i have two and i'm a little confuse.
so this is my code,
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
while($row = mysql_fetch_array($query)){
echo "'".$row['provider_id']."':'".$row['company_name']."',";
}
I am using this to generate javascript code for Jeditable.
I need to take out the last comma, so I can wrap up with the rest of the code...
I like to use an array:
$query = ...;
$out = Array();
while($row = mysql_fetch_assoc($query)) {
$out[] = "'".$row['provider_id']."':'".$row['company_name']."'";
}
echo implode(",",$out);
It looks like you're trying to output some kind of JSON though, so perhaps you could use:
$query = ...;
$out = Array();
while($row = mysql_fetch_assoc($query)) {
$out[$row['provider_id']] = $row['company_name'];
}
echo json_encode($out);
So i usually just stack things like this into an array and then implode:
$parts = array();
while($row = mysql_fetch_array($query)){
$parts[] = "'".$row['provider_id']."':'".$row['company_name']."'";
}
echo implode(',', $parts);
That said it looks like youre outputting the body of a JSON string. If so you shouldnt manually build it you should use json_encode instead.
$data = array();
while($row = mysql_fetch_array($query)){
$data[$row['provider_id']] = $row['company_name'];
}
echo json_encode($data);
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
$result = "";
while($row = mysql_fetch_array($query)){
$result .= "'".$row['provider_id']."':'".$row['company_name']."',";
}
$result = rtrim($result, ',');
OR
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
$result = array;
while($row = mysql_fetch_array($query)){
$result[] = "'".$row['provider_id']."':'".$row['company_name']."',";
}
$result = implode(',' $result);
OR
get count of records (mysql_num_rows($query)) and when you hit last row just do what you want