Loading table column names with spaces with ms sql in php - php

I am using php to connect to my ms sql server. I am using php 5.3, using the microsoft database dll to connect. The problem i have is that in my database, i have tables wich there column's names have blank spaces, mean there is a table called bills whit a column named "Shipper Code" or "Shipper name"... something like that... i want to know how i can read the rows using fetch_object
while($row = sqlsrv_fetch_object($stmt))
{
echo "<li>" . $row->Code . $row->MasterId . "</li>";
echo "<li>". $row->Shipper Code . "</li>"; <== HERE HOW I MUST PUT SHIPPER CODE?
}
Do i must use () or {} or []...
thank you in advanced!

You should avoid the usage of spaces in column or object names. If you wish to separate the strings then use an underscore _ instead. This will retain the readability and ensure that the item can be accessed properly.
Other than that you can use the following:
while($row = sqlsrv_fetch_object($stmt)) {
$results[] = $row;
echo "<li>" . $results['Code'] . $results['MasterId'] . "</li>";
echo "<li>". $results['Shipper Code'] . "</li>";
}

I've got the answer. I only need to use "{" and "}". Something like this
while($row = sqlsrv_fetch_object($stmt))
{
echo "<li>" . $row->{"Num"} . $row->{"MasterId"} . "</li>";
echo "<li>". $row->{"Shipper Code"}. "</li>";
}
Thanks for all your help.

Related

Cannot selecting first row on database php pdo mysql

I couldn't understand what is wrong here. I cannot take the first row on database. I also cannot the data which are single according to $ara parameter. Here are the codes:
$ara =$_POST['ara'];
$query=$db->prepare("SELECT * FROM uyeler WHERE
name like '%".$ara."%' or
surname like '%".$ara."%' or
email like '%".$ara."%'
");
$query->execute(array());
if ($num_row = $query->fetchAll(PDO::FETCH_BOTH)){
echo "<li><a href='profil.php?id=".$num_row['user_id']."'>" .$num_row['name']. " " .$num_row['surname']."</a></li>";
}else{
echo "Hiç birşey bulunamadı!";
}
You need to correct your array call:
from
$num_row['user_id']
To
$num_row[0]['user_id']
Your code is tested and works, nice job.
what you need to correct is the following line:
echo "<li><a href='profil.php?id=" . $num_row[0]['user_id'] . "'>" . $num_row[0]['name'] . " " . $num_row[0]['surname'] . "</a></li>";
If you face such problem in the future, it is a good practice to var_dump the results like:
var_dump($num_row);
This will give you a total raw picture of the content.

How to create a numbered list inside a table with PHP

I'm executing this type of code inside a table.
while($row = mysql_fetch_array($result))
{
echo "" . $row['id'] . "";
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
}
Which works great and gives me a numbered list (1 Mike Volvo, 2 Mike Ford) for example. The problem is that I now have multiple users in the same table so there are gaps in the list where someone else's entry is since i'm using auto incremented ID for numbers.
So assuming I have 5 entries I obviously want it to be 1-5 but right now its 5-30-45-65 or whatever.
Anyway I looked around for a solution and I found $number = 1; $number++; to be effective and it kind of works and gives me a 1-5 list independent of whatever the ID's are.
The problem is that when I reverse the list using ORDER BY xxx desc the last entry becomes 1 and the first entry becomes 5. But I always want the first entry to remain 1 and the last entry to be 5.
Do you have any ideas on how to create this function using PHP?
You need to simple user $number as you mentioned as in the following code
$number = 1;
while($row = mysql_fetch_array($result))
{
echo $number;
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
++$number;
}
Even if in your SQL you have order by it does not matter in that case.
From what I understand of your question... You are on the right track using the following code...
$LastId ='';
$number = 0;
while($row = mysql_fetch_array($result))
{
if($LastId != $row['id']){
$number++;
}
echo $number;
echo "" . $LastId = $row['id'] . "";
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
}
So...
If the 'id' isn't the same as the last 'id' (not the same user) it will increment it by 1. Otherwise it will stay the same.
Hope this is what you looking for and helps.
Although this is pretty trivial in php, I would not use php for it. You can easily do this in html:
echo "<ol>";
while($row = mysql_fetch_array($result))
{
echo "<li>" . $row['name'] . $row['car'] . "</li>";
}
echo "</ol>";

How do I use quotation marks properly when echoing properties and attributes through PHP and SQL)?

while ($row = $arr_result ) {
echo "<option value='" . $row['fallacytype'] . "'>" . $row['fallacytype'] . "</option>";
}
I am trying to create a and menu in html by using the string values in my SQL table.
I've confirmed that the rest of my code (not shown here, but can be provided if needed to diagnose), is working. The only thing that goes wrong is that my loop up there becomes infinite (i know this because it creates infinite sql value).
I've looked up and tried all the variations of quotation marks to no avail.
I've checked in w3shool.com, and other video tutorials that my loop syntax is correct.
It seems that the $row variable is not incrementing or moving on to the next value in the loop for some curious reason.
Thanks in advance.
You have to loop via following way for all the elements:
foreach($arr_result as $row) {
echo "<option value='" . $row['fallacytype'] . "'>" . $row['fallacytype'] . "</option>";
}

PHP MySql Query performance with variable declaration

Alright - this may be a dumb question, but my desire for perfection is fueling me, so, if it is stupid and pointless, just let me know.
I have a mysql query where I fetch rows from a database table, and I need the value in some shape or form for all the columns I collect. Look at the following sample code (this could use an array to gather output instead - the fundamental question should be the same):
$query="SELECT A,B,C,D FROM table1 WHERE 'X'='Y'";
$result=mysql_query($query,$resource);
while($row=mysql_fetch_assoc($result)){
$var1=$row['A']; $var2=$row['B'];
echo '<li class="' . $row['C'] . '" id="' . $row['D'] . '">';
}
My question is as follows:
I only need $row['A'] and $row['B'] once since they are the same value for every row in this case, but I do need them once. The other values will be different for every row, and I need them as well, as in the example.
Is there a problem with continuously setting the variable throughout the loop? Or is it better to use if(!isset...? or is there some other way to do this? Or is the performance hit so minimal as to make this question irrelevant?
Thanks
why not just set the variables outside the loop.
$result=mysql_query($query,$resource);
$result_new = mysql_fetch_assoc($result);
$var1 = $result_new['A']; //considering $result_new['A'] always exists
$var2=$result_new['B']; //considering $result_new['B'] always exists
while($row=$result_new){
echo '<li class="' . $row['C'] . '" id="' . $row['D'] . '">';
}
Really don't bother with that. Unless you're looping over millions of records.
If you need more speed, don't echo in the while loop:
$result=mysql_query($query,$resource);
$string ='';
if ($result) {
$row=mysql_fetch_assoc($result);
$string .= '<li class="' . $row['C'] . '" id="' . $row['D'] . '">';
$var1=$row['A'];
$var2=$row['B'];
while($row=mysql_fetch_assoc($result)){
$var1=$row['A']; $var2=$row['B'];
$string .= '<li class="' . $row['C'] . '" id="' . $row['D'] . '">';
}
}
echo $string;
Two more things. Don't use mysql_*: Use of this extension is discouraged!
And (like I thought someone else said) if a/b is always the same, you should rethink you're database structure.
Edit:
To give a real answer to your question:
I did a small test.
Setting the value on each loop (1,000 times) takes 0.00027s. Checking inside the loop if the value was already set takes 0.00030s. So just setting it each time is even faster than checking.
the performance hit in this scenario is irrelevant, in fact i would speculate if if(isset..) guards would take a tiny bit longer - only scenario I can think of, where this question would matter would be a situation where you assign (larger) objects or arrays to $var1 and somehow force php to make a copy, not a reference
if it bothers you for aesthetic reasons (I could relate ;)), you could use a do - while loop:
//assuming you always get at least one result
$result=mysql_query($query,$resource);
$row=mysql_fetch_assoc($result);
$var1=$row['A']; $var2=$row['B'];
do {
// the loop stuff - stringify or echo the list
} while ($row=mysql_fetch_assoc($result)) ;
you can do it with a do-while expression
$query="SELECT A,B,C,D FROM table1 WHERE 'X'='Y'";
$result=mysql_query($query,$resource);
if($row=mysql_fetch_assoc($result)) {
$var1=$row['A']; $var2=$row['B'];
do {
echo '<li class="' . $row['C'] . '" id="' . $row['D'] . '">';
} while($row=mysql_fetch_assoc($result));
}

PHP - Fill pulldown with array results from web service method

I currently have a web service (C#) that returns an array of strings to my client in PHP.
I want to fill a pulldown with the string elements in the array results.
With the following code, no items appear in the pulldown.
<select name="name-list" id="name-list" class="pulldown" onchange="exportName();" >
$client = new SoapClient("http://localhost/MyService.asmx?wsdl", array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));
$res = $client->GetServiceArray()->GetServiceArrayResult;
$array = (array)$res->ArrayOfString;
foreach($array as $val)
{
echo "<option value=\"" . $val . "\">" . $val . "</option>\n";
}
?>
I have also done var_dump($res) to make sure the web service is returning the data. I can confirm that the values show in the page source.
It seems to be doing nothing on this line:
$array = (array)$res->ArrayOfString;
Is there an alternative way of doing this?
Please can I have some advice on how to make the items appear.
Thank you.
It should be
foreach($array as $val)
{
echo "<option value='$val'>$val</option>";
}
Most likely you need to do this
echo "<option value=\"" . $val . "\">" . $val . "</option>\n";
And don't forget to escape $val, because if there are any quotation characters inside, they will break your HTML. You can use
$val = addslashes($val);
Can it be $client->GetServiceArray()->GetServiceArrayResult is actually a function?
It should be $client->GetServiceArray()->GetServiceArrayResult() then.
In any way you should first check, if $res does contain any data: var_dump($res);

Categories