I'm not too sure what's going on here but I'm trying to echo a value from mysql and when I do, it just shows double for some reason
Code:
$result = MySqlQuery('SELECT value FROM table WHERE id=1');
$value = mysqli_fetch_assoc($result);
echo implode($value);
It displays 7373, the value is 73 in the DB.
I also tried echoing * instead of value, it also displays the entire row double.
Removing the echo there just displays nothing anymore so it's not like it's being echoed through another function either so I'm confused
Also the MySqlQuery() function is used by pretty much everything else on the site where it doesn't display double results as well
mysqli_fetch_array returns an array with twice as many elements as the columns you select by default (each column is represented twice). I assume that the mysqli_fetch_assoc in your code is a typo.
To solve the problem, either use mysqli_fetch_assoc instead or pass one of MYSQLI_ASSOC and MYSQLI_NUM as the second parameter to mysqli_fetch_array. As a rule of thumb, use mysqli_fetch_assoc unless you know you need something else.
Related
I am receiving the last sort value out of 3 tables, the function works perfectly but I can't +1 the value that I return from the function. I do not get ANY errors, do you guys see what I am doing wrong?
What did I try?
Using intval in the function
Using brackets around the calculation
A lot more
Code link: http://pastecloud.net/ZDR2JgSbIN
die(lastSort());
Displays 1, or whatever the last value is.
$last = lastSort();
$new = $last+1;
die($new);
Displays a white page
What is wrong?
Referencing to this answer:
die() is the same as exit(), looking at the exit docs it takes 1
parameter, $status, the parameter information states.
...If status is an integer, that value will be used as the exit status
and not printed.
So, if you want to die() an integer, you should first convert it to a string. Like this way: die( (string)$new );.
If you are absolutely sure that the function return the correct value, you can try to convert the result to an int.
$last = intval(lastSort());
$new = $last+1;
But if lastSort returns a array (empty or non-empty), intval will return 0 or 1 respectively. Which could mess up you whole logic.
I am having a while loop in my function for getting the values 1 by 1 but i am getting the only 1 value
while($row= mysqli_fetch_array($this->result))
{
$image=$this->getEventDetails($row['Album_top'],'Event_image');
$alert.="<div class=facBox>
<a href='gallery.php?id=$row[Id]' style=margin-top:0px;>
<img src='admin/customer/eventgallery/$image' alt=''>
</a>
<div class=clear></div>
<a href='gallery.php?id=$row[Id]' style=margin-top:0px;>$row[Album_title]</a>
</div>";
}
I am getting the image name from another function that is
function getEventDetails($id,$fieldname)
{
$get="Select * from sarnaevent where Id='$id'";
$this->result=mysqli_query($this->con,$get);
$row=mysqli_fetch_array($this->result);
return $row[$fieldname];
}
Now i am getting the only value from the loop my $alert is having only one facbox. if i remove this line
$this->getEventDetails($row['Album_top'],'Event_image');
It works fine but i want this line to get the image name.
Inside getEventDetails(), you assign $this->result, overwriting the previous contents of $this->result. Since that occurs inside the while loop where the previous result is being used, the loop exits because there are no further results to retrieve from the inner fetch.
User a different temporary result set inside teh getEventDetails() method.
function getEventDetails($id,$fieldname)
{
$get="Select * from sarnaevent where Id='$id'";
// Different variable name...
$temporary_result = mysqli_query($this->con,$get);
$row=mysqli_fetch_array($temporary_result);
return $row[$fieldname];
}
In general, I would question the need to be storing a transient result resource into the object's $this->result for most purposes. In any case where you're using that inside a method, you are probably better off using a variable scoped only to the method, which lives only for the lifetime that result set is being used.
Please use caution when sending $id directly into the query. Although I suspect it is known to be an integer variable, and therefore it won't break the SQL, it's a good idea to get into the habit of using prepare()/execute() to prevent SQL injection.
One final point of caution: When placing the string variable into your HTML markup, be sure to use htmlspecialchars() to escape it against malforming the HTML. (If the Id is known to be an integer, it isn't necessary there)
"...<a href='gallery.php?id=$row[Id]' style=margin-top:0px;>" . htmlspecialchars($row['Album_title']) . "</a>..."
Instead of using mysqli_fetch_array() try using mysqli_fetch_assoc (see: http://php.net/manual/en/mysqli-result.fetch-assoc.php); the returned array will be associative (key-value pairs) where the keys are the column names.
You're blowing out your original query.
First, you do a query, and assign its result to $this->result. You then iterate across those results. For the first row, you immediately make a new query, overwrite $this->result with the new results, and then try to continue... but you've just replaced the rest of your results with the single result from the event details query, so mysqli_fetch_array doesn't find any more results.
Solution: use a separate variable for that result. A local one should be fine, since you don't use it outside that function. You may also need to use a separate connection; I'm not sure of that. Try it with just changing $this->result in getEventDetails() to use something like $eventResult instead.
I am working on a system to transfer a table into an array over PHP and html forms. In doing so I have to echo the users ID into a hidden element on the table. Here is where I am stumped:
$sqlb = mysql_query('SELECT * FROM table_row WHERE tid = 1');
$numb = 0;
while($rowsres = mysql_fetch_array($sqlb))
{
....
echo('<td style="text-align: center;"><input type="checkbox" value="'.$rowres['rid'].'" name="list[]" /></td>');
echo $rowres['rid'];
echo('<td>'.getData(1, $rowsres['rid'], 1).' '.getData(1, $rowsres['rid'], 2).'</td>');
....
}
To explain the code a little and what is happening:
I start by looping through my database.
The first .... is me doing some simple filtering of the results
Then the issues start. The first echo is me trying to put a hidden value of the row ID.
The value will NOT write. I've tried print, echo, I've tried assigning it to other variables but for whatever reason it will not write.
To make things more confusing: When I use that SAME variable in the third echo statement, it works! That function getData just accesses a database and returns the first and last name of the user, which it IS doing.
I'm extremely confused and have never come across an issue like this before.
Other things:
When I dump the $rowres it returns null, same with $rowres['rid']
I've tried echoing the variable in multiple places and none will echo but again, it works with the function because the function is returning the correct first and last name to each user.
You're using two different variable names:
$rowsres is what your assigning things to in the loop and what your passing into getData.
$rowres (note only one 's') is what you're trying to echo. This is a completely separate variable which has nothing assigned to it.
I have seen the following while loop condition in many examples and even I have used it in many times. I know how it works and how to use it. But as i code the condition here doesn't make any sense.
As I see the code, the condition is like it's always true. Just like while(1). Because to the *mysql_fetch_assoc()*, the same data is passed is passed all the time. So the condition is a constant.
while($arr = mysql_fetch_assoc($data))
{
//other code
}
Now, where Am I Wrong ????
Each call to mysql_fetch_assoc gets the next row from the result set. If there is no row anymore it returns false and the loop terminates.
$data is a resource data type and will probably keep the state about which row was fetched last.
This is not so unusual, even arrays have an internal pointer to the current element which can be manipulated using certain array functions.
From the PHP website, mysql_fetch_assoc "Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows."
Therefore, whenever there are still rows available, mysql_fetch_assoc will return an associated array of the row contents, will increment the pointer to the next row and the while loop will execute.
When there are no more rows mysql_fetch_assoc will return FALSE and the loop will break.
Also note that as of PHP 5.5.5 this function will be deprecated.
How do I echo a simple string from a MySQL Query?
I'm trying trying to accomplish this with the following code but it is not working...The data I am pulling is fine so I know that my mysql_query is working (I've checked that via a different URL GET method.
<?php
$myQuery = mysql_query("fetch some stuff....");
$myResult = mysql_fetch_object($myQuery);
echo $myResult;
you need to know what is returned type. in what your doing you assume that it printable but most of what db queries return are either in object form or an array
try doing a
echo "<pre>" ,print_r($myResult, TRUE),"</pre>";
First of all use var_dump($myResult) to see the data and it's structure.
Since it's an object it will have properties named as the columns returned by the SELECT statement you used.
echo $myResult->column_name; // Should work fine
Usually if echo $variable; doesn't work it means that the variable is either en empty string '' or a null value NULL or a false value FALSE which all show "nothing" when echoed.
But when using var_dump() on them you get a report of the type of data and size of it.
Providing your query is correct, it looks like your php tags are incorrect:
<?php ?>
P.S. It might help if you post the actual query so it can be troubleshooted here. It's hard to ask why something is not working and get an answer if you don't show any of it.
First, var_dump($myResult);. If you see NULL, your query is failing. If you see a big block o' jumbled text, the query is, in fact, working. Since you are echoing $myResult, it is no surprise that nothing is being output, as you are trying to echo the object directly rather than the property you want. Try echoing $myResult->myColumn;
Also, please use MySQLi or PDO, as php_mysql is deprecated.