how to show the result of a query on my website - php

so I want to show the result of my query, and I can't seem to find anything that works.
this is what I have so far and I have no idea why it won't work.
daydream festival <?php
$queryday="SELECT `tickets_MAX`-`tickets_VERKOCHT` AS `tickets_over` FOR `evenementdata` WHERE `ev id`=1";
$resultday=mysql_query($queryday);
$rowday = mysql_fetch_assoc($resultday);
echo $rowday;?> tickets left

Building on other reasons why this won't print anything like your rows not existing, or misnaming things.
If I'm not mistaken mysql_fetch_assoc returns an array, echo does not print arrays. You need to use print_r($rowday); - Assuming that $rowday contains data.

I'm making a few assumptions here, but I think it's likely that you should do it like:
<?php
$queryday="SELECT `tickets_MAX`-`tickets_VERKOCHT` AS `tickets_over` FROM `evenementdata` WHERE `ev_id`=1";
$resultday=mysql_query($queryday);
$rowday = mysql_fetch_assoc($resultday);
print_r($rowday);
?>
That being said, I would strongly recommend not using the mysql_ functions, since they are now being depreciated. You should do some research into The PHP Data Objects (PDO).

Related

populating fields in PHP with MySQLi Array

Can I get a quick reality check?
The idea that I'm working on is that I have text in a HTML template that looks like {Field1} and I want to populate the values for that field from a MySQLi connection where the database field is Field1.
So in theory I should get the data in an array, then cycle through that array, (adding {} and) replacing the text in the HTML with the required values.
Is there a more elegant solution to my task here or is what I'm planning pretty much it?
Does anyone have any advice or tutorials on handling the associated array returned by MySQLi? Much appreciated. I'm still learning and I'm just banging my head on this problem for a bit too long.
Without seeing your source for PHP, it is VERY difficult to answer this question,
I believe what you want to do can be achieved using *array_map*
Following would add the braces as you wish around each element in the array
// $row = .... your getter from mysqli connection
$row = array_map("apply_braces", $row);
// end of loops and stuff -- begin functions below
function apply_braces($field) {
return '{' . $field . '}';
}
The above will set all values in $row, to "{" . $somevalue . "}", so whatever the value of the item in the array is, it will add the braces around it using the custom function.
Sure.
Check failed.
Do not re-invent the wheel.
Your idea on templates is wrong. Please learn how to use already existing templating systems before trying to create your own.
Does anyone have any advice or tutorials on handling the associated array returned by MySQLi?
Definitely NO.
Simply because there is absolutely nothing special in the associative arrays returned from mysql. They are exactly the same as any other associative array in PHP, no difference.

making a comfortable php multidimensional array from database

i got a code like this:
include 'dbconnect.php';
mysql_query("SET NAMES utf8;");
$sql = mysql_query("SELECT * From courseEnglish");
$courseInfo = array();
while ($row_course = mysql_fetch_assoc($sql))
$courseInfo[] = $row_course;
it gives me a php array from the db, but in order to present the data i need to use:
echo $courseInfo[2]['Course'];
i would like for the array to be so i can just type somthing like:
echo $courseInfo[2][2];
is there an option for changing the code so ill have an array i can loop on with just indexes?
I think that you're looking for mysql_fetch_row, which does the same as mysql_fetch_assoc, except for the fact that it returns a numerically indexed array. here's the link to the docs.
Having said that, you might want to consider ditching the deprecated mysql_* extension, in favour of either mysqli_* (the i stands for improved), or the fully OO-style PDO extension. Check the red box at the top of any mysql_* function's manual page, it contains links to the preferred counterpart-function...
But all things considered, looking at your query (SELECT * FROM ...), I'd not use a numerically indexed array at all to get the results. If your table contains more than 1 field, you'll hit trouble sooner or later, when trying to work out what piece of data is coming from what field. Seriously... just use an assoc array, or even better: use objects.
Suppose you change the table layout (add/remove certain fields?), it'll be hell to maintain your code in that case. Still, it's up to you to decide, but I thought I'd might mention this.
Maybe this..?
foreach ($courseInfo as $course_index = > $course_content)
foreach ($course_content as $field)
echo $field;
cheers
Try this :
while ($row_course = mysql_fetch_array($sql))

Echoing string from a mysql query - PHP

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.

Using Same Variable in Multiple PHP `while` Loops

I have a PHP page with multiple mysql_query instances. Almost every one uses a while loop to retrieve multiple rows of data.
As I keep a snippet of this often used code, and the example I was taught with, used the variable $row, I have multiple instances of the following on my page:
while ($row = mysql_fetch_assoc($foo_data)) {
$barArray[] = $row['barValue'];
}
I even have an instance of $row = mysql_fetch_assoc($foo_data) without a while loop.
I'm wondering if the multiple uses of $row as a variable on a single page, is all right?
The PHP is functioning fine, but I always want to be sure that my code is proper and conforms to standard rules.
Thank you.
yes, it's all right.
it's all right to use the same spoon when you're eating soup.
Yes that is fine. but if you try:
var_dump($row);
at the end of your code -- it will only return that last value of $row
Yup, not a problem at all.
Just like always using $i as your count in a for loop, you can repeatedly use $row to no ill effect.

Selecting only the first item of an xpath result set in PHP

I am currently achieving the desired outcome with two PHP statements:
$thisBlarg = $xmlResource->xpath('//blarg[#ID='.$someBlargID.']');
echo $thisBlarg[0]->name;
But, not wanting to settle for second best, I'd really prefer this to be one statement, but PHP doesn't like this:
echo $xmlResource->xpath('//blarg[#ID='.$someBlargID.']')[0]->name;
And for good reason. But I can't find a way to force an xpath query to return the result directly. Any suggestions?
Try this
echo current(($xmlResource->xpath('//blarg[#ID='.$someBlargID.']')))->name;

Categories