Jump to content

organizing my query from the database


CyberShot

Recommended Posts

I have a test database set up that I am working on. Right now, just some names like so

 

firstName

lastName

companyName

 

I have used phpmyadmin to insert names in the database. Right now just three names are inserted. One first and last name, one first name and one company name

 

I am getting the results back in an associative array. How can I echo out the information with the break tag, or one field at a time? if I use a break tag, then the loop adds in extra blank lines when it gets to the first name and it's null. Can I test for null values and do it that way? I am a beginner so I have no idea what I am doing.

 

$result = $mysql->query("select * from names") or die($mysql->error);
if($result){
while($row = $result->fetch_assoc()){
	$name = $row['firstName'];
	$lastName = $row['lastName'];
	$company = $row['companyName'];

		echo $name . " ";
		echo $lastName . " ";
		echo $company . " ";
}

}

Link to comment
Share on other sites

a break statement is to get out of a loop early.

 

what you want is EOL terminators

and that depends on your OS/Browser

 

echo "This will work in  a browser <br />";
echo "This will work from cli \n"; // \n linux EOL, \r\n Windows EOL, \r Mac EOL
echo "This will also work in cli". PHP_EOL;
echo "This will work in browser and cli <br />". PHP_EOL;

 

also note that php also has the function nl2br, which converts the EOL \n to a <br /> tag

Echo nl2br("This String\nWill be split\nInto 3 lines");

 

Link to comment
Share on other sites

that kind of thing isn't working for me. I end up with this

 

joe black

john doe

 

 

sears

 

 

you see, there is a big gap between sears and the first last name. The reason is because of the <br> tag that I throw into the code to put each result row onto a new line in my code. I am trying to figure out how to get the code to ignore the nulls and put everything on the next line instead of several lines down

Link to comment
Share on other sites

I have been continuing to work on this issue. I took the <br> tags out trying to fix it. You are correct that my code doesn't not have any <br> tags in it. That is why I told you what I get when I use the br tags. yes, right now it spits the results out on one line. I am trying to figure out how to put each result on a seperate line without the code adding in extra blank lines between results as I showed you happens in the post above when I use br tags in this fashion

 

echo $firstName . " " . $lastName . "<br />";
echo $companyName;

 

If I do the above, I get this

 

john doe

Chrissy

 

 

sears

 

it gives me that because I left the last name blank on the second entry or whatever combination I use. It puts the company name further down the list with blank lines inbetween

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.